import datetime
import xml.dom.minidom
import random
import re
import sys
import tkinter as tk

# 设置主窗口
window = tk.Tk()
# 设置主窗口标题栏
window.title('自动添加报文序列')
window.geometry('800x500')

l01 = tk.Label(window, text='清输入原报文:')
l01.place(x=30, y=10, anchor='nw')
t01 = tk.Text(window, height=12)
t01.place(x=30, y=30, anchor='nw')


def increase_copy_res():
    content = t01.get('1.0', 'end-1c')  # 获取第二个窗格的内容
    newcontent = [line for line in content.splitlines() if line.strip()]  # 去掉空行
    datasource = '\n'.join(newcontent)

    tree = xml.dom.minidom.parseString(datasource)  # minidom模块只支持utf-8格式文档

    num = str(random.randint(10000000, 99999999))

    # 获取某个标签
    ele01 = tree.getElementsByTagName('id')[0]
    newid = '202402213000' + num
    ele01.firstChild.data = newid

    ele02 = tree.getElementsByTagName('msgid')[0]
    ele02.firstChild.data = newid;

    ele03 = tree.getElementsByTagName('PackNo')[0]
    ele03.firstChild.data = num;

    ele04 = tree.getElementsByTagName('TraNo')[0]
    ele04.firstChild.data = num;

    ele05 = tree.getElementsByTagName('VouNo')[0]
    ele05.firstChild.data ="sqe"+ num;




    root = tree.documentElement
    t02.delete('1.0', 'end-1c')  # 清除窗格内容后再赋值
    t02.insert('insert', '<?xml version="1.0" encoding="GBK"?>\n')
    t02.insert('insert', root.toxml())  # 给第二个窗格赋值

    content01 = t02.get('1.0', 'end-1c')  # 获取第二个窗格的内容
    window.clipboard_clear()  # 清除剪贴板内容
    window.clipboard_append(content01)  # 给剪贴板赋值
    window.update()  # 更新剪贴板


b = tk.Button(window, text='增加序列并复制到剪贴板', width=20, height=3, command=increase_copy_res)
b.place(x=620, y=180, anchor='nw')

l02 = tk.Label(window, text='结果预览：')
l02.place(x=30, y=210, anchor='nw')

t02 = tk.Text(window, height=16)
t02.place(x=30, y=230, anchor='nw')

l03 = tk.Label(window, text='注意：仅对id增加序列值')
l03.place(x=30, y=460, anchor='nw')

window.mainloop()



